import { isH5 } from '@/utils/platform'

const imgBaseUrl = import.meta.env.VITE_IMG_BASEURL // 图片请求路径

interface ImgAssetsOptions {
  noCache?: boolean
}

export type ImgAssetsFn = (filePath: string, options?: ImgAssetsOptions) => string

export function useImgAssets(filePath: string, { noCache = false }: ImgAssetsOptions = {}) {
  // 去掉 filePath 前面的 @ 符号
  if (filePath.startsWith('@')) {
    if (isH5) {
      filePath = `src${filePath.slice(1)}`
    }
    else {
      filePath = `${filePath.slice(1)}`
    }
  }

  const fileUrl = `${imgBaseUrl || ''}${filePath}`
  if (noCache) {
    return `${fileUrl}?t=${Date.now()}`
  }
  return fileUrl
}

export const useAssets: ImgAssetsFn = useImgAssets
